<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form inline>
        <el-form-item label="名称">
          <el-input v-model="keyword" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="get_list">搜索</el-button>
          <el-button @click="(keyword = ''), get_list()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="open_add_dialog" plain>新增</el-button>
      <el-table :data="list">
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column label="操作" width="200px" v-slot="{ row }">
          <el-button type="primary" @click="open_edit_dialog(row)" size="mini"
            >修改</el-button
          >
          <el-button type="danger" @click="del_row(row.id)" size="mini"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      @close="reset_list_e"
      center
      width="1200px"
    >
      <el-form :model="list_e" ref="list_e_ref" inline>
        <el-form-item
          v-for="(
            {
              form_item_prop: { label, prop, required },
              component_name,
              component_prop,
              handler,
            },
            index
          ) in list_e_c"
          :key="index"
          v-bind="{ label, prop, required }"
        >
          <component
            :is="component_name"
            v-bind="component_prop"
            v-model="list_e[prop]"
            :placeholder="label"
            v-on="handler"
          >
            <template v-if="component_name == 'el-select'">
              <el-option
                v-for="i in component_prop.option_data"
                :key="i.id"
                :value="i[component_prop.option_prop.value]"
                :label="i[component_prop.option_prop.label]"
              ></el-option>
            </template>
          </component>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="detailList">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column width="200px" label="产品" v-slot="{ row }">
          <span>{{ row.material.name }}</span>
          <el-button
            style="margin-left: 20px"
            type="primary"
            size="mini"
            @click="open_material_dialog(row.id)"
            >选择</el-button
          >
        </el-table-column>
        <el-table-column
          v-for="item in list_detailList_c"
          :key="item.prop"
          v-bind="item"
          width="140px"
        >
          <template v-if="item.cn" #default="{ row }">
            <component :is="item.cn" v-bind="item" v-model="row[item.prop]" />
          </template>
        </el-table-column>
        <el-table-column width="200px" label="操作" v-slot="{ $index }">
          <el-button type="success" size="mini" @click="add_detail_row($index)"
            >添加</el-button
          >
          <el-button type="danger" size="mini" @click="del_detail_row($index)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
    <el-dialog
      width="1400px"
      :visible.sync="visible_material"
      title="选择物料档案"
      center
    >
      <el-row>
        <el-col :span="6">
          <el-tree
            :data="list_material_category"
            :props="{ value: 'id', label: 'name' }"
            :expand-on-click-node="false"
            @node-click="(val) => get_material_list(val.id)"
            :default-expand-all="true"
          ></el-tree>
        </el-col>
        <el-col :span="18">
          <el-table :data="list_material">
            <el-table-column
              v-for="item in list_material_c"
              :key="item.prop"
              v-bind="item"
            ></el-table-column>
            <el-table-column width="200px" label="操作" v-slot="{ row }">
              <el-button
                type="primary"
                size="mini"
                @click="select_material(row)"
                >选择</el-button
              >
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getProductModelingList, getProductModelingDetail, getMaterialCategoryList, getMaterialList } from '@/api_zc/mes/product-modeling'
import { getBomList, getBomDetail, editBom, addBom, delBom } from '@/api_zc/mes/bom'
import config_mixin from './config_mixin'
export default {
  mounted () {
    this.get_list()
    this.get_unity_list()
    this.get_material_list()
    // this.get_product_modeling_detail_list()
  },
  mixins: [config_mixin],
  methods: {
    async get_material_list (pid = 0) {
      const { data: { content } } = await getMaterialList({ pid })
      this.list_material = content
    },
    async get_list () {
      const { data: { content, total } } = await getBomList({ keyword: this.keyword, ...this.pager })
      this.list = content
      this.pager.total = total
    },
    async get_unity_list () {
      const { data: { content } } = await getProductModelingList({ keyword: '', page: 1, limit: 999 })
      this.list_e_c.find(item => item.form_item_prop.label == '产品模型').component_prop.option_data = content
      const { data } = await getMaterialCategoryList()
      this.list_material_category = data
    },
    async get_detail_list (id) {
      const { data } = await getBomDetail({ id })
      this.detailList = data
    },
    async get_product_modeling_detail_list (id) {
      const { data } = await getProductModelingDetail({ id })
      console.log(data)
      this.detailList = data
    },
    async del_row (id) {
      await delBom({ ids: [id] })
      this.$message.success('删除成功')
      this.get_list()
    },
    open_edit_dialog (row) {
      this.visible = true
      this.is_add = false
      this.title = '修改BOM管理'
      this.$nextTick(async () => {
        await this.get_detail_list(row.id)
        Object.assign(this.list_e, { ...row, product: row.productModel.product, productModelId: row.productModel.id })
      })
    },
    open_add_dialog () {
      this.visible = true
      this.title = '添加BOM管理'
      this.is_add = true
    },
    reset_list_e () {
      this.$refs.list_e_ref.resetFields()
      this.detailList = []
    },
    open_material_dialog (id) {
      this.id = id
      this.visible_material = true
    },
    add_detail_row (index) {
      this.detailList.splice(index + 1, 0, {
        id: Math.floor(Math.random() * 10000),
        material: {
          name: '',
          model: '',
          specification: '',
          unit: ''
        },
        quantity: 0,
        remark: ''
      })
    },
    del_detail_row (index) {
      this.detailList.splice(index, 1)
    },
    select_material (row) {
      if (typeof row.category == 'object') {
        delete row.category
      }
      this.detailList.find(item => item.id == this.id).material = row
      this.visible_material = false
    },
    async submit () {
      this.$refs.list_e_ref.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请完善表单')
        }
        this.detailList = this.detailList.map(item => {
          return {
            ...item,
            materialId: item.material.id,
            quantity: item.quantity
          }
        })
        if (this.is_add) {
          await addBom({ ...this.list_e, detailList: this.detailList })
          this.$message.success('添加成功')
        } else {
          await editBom({ ...this.list_e, detailList: this.detailList })
          this.$message.success('修改成功')
        }
        this.visible = false
        this.get_list()
      })
    },
    change (val) {
      this.get_product_modeling_detail_list(val)
    }
  },
  data () {
    return {
      list: [],
      keyword: '',
      pager: {
        page: 1,
        limit: 10,
        total: 0
      },
      visible: false,
      title: '',
      is_add: false,
      detailList: [],
      list_product_model: [],
      list_material: [],
      list_material_category: [],
      visible_material: false,
    }
  },
}
</script>

<style>
</style>